package com.example.blog.config;

import com.example.blog.hander.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMVCConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //跨域配置，不可设置为*，不安全，前后端分离项目，可能域名不一致
        //本地测试 端口不一样 也算跨域
        registry.addMapping("/**").allowedOrigins("http://localhost:8080");

    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 暂时设置拦截路径为 /test 之后再添加需要拦截的路径
        registry.addInterceptor(loginInterceptor).addPathPatterns(
                "/test", "/comments/create/change", "/articles/publish"
        );
    }
}
